<?php

namespace Wechat\Controller;

use Think\Controller;

class TaskController extends Controller  {

    public function abandonOrder() {

        $orderMap['status'] = C("ORDER_STATUS.WAITING_PAY");
        $orderMap['create_time'] = array("elt",
            date("Y-m-d H:i:s", strtotime("-1 minute")));

        $orderList = D('Order')->where($orderMap)->select();

        if(!isNullOrEmpty($orderList)) {

            $orderIdList = rtrim(implode(',',array_column($orderList,'order_id')),',');

            //改变状态
            $sql = "UPDATE __ORDER__ 
                SET status = ".C('ORDER_STATUS.SYSTEM_ABANDON')."
                WHERE order_id IN (" .$orderIdList. ")";

            M()->execute($sql);

        }

    }

    public function unChooseDraw() {

        $orderMap['status'] = C("ORDER_STATUS.WAITING_CHOOSE");
        $orderMap['TIMESTAMPDIFF(MINUTE,update_time,NOW())'] = array('egt',15);

        $orderList = D('Order')->where($orderMap)->select();

        if(!isNullOrEmpty($orderList)) {
            $groupIdList = rtrim(implode(',',array_values(array_unique(array_column($orderList,'group_id')))),',');
            $orderIdList = array_column($orderList,'order_id');

            //修改订单状态
            $orderMap['order_id'] = array('in',$orderIdList);

            $orderData['status'] = C('ORDER_STATUS.WIN');
            $orderData['update_time'] = getCurrentDate();

            D('Order')->where($orderMap)->save($orderData);

            //改变余额
            $sql = "UPDATE __CUSTOMER__ c
                LEFT JOIN __GROUP_MEMBER__ gm ON c.customer_id = gm.customer_id
                SET c.available_assets = ROUND(c.available_assets + gm.reward * ".C('DEFAULT_REWARD_FEE').",2),
                c.total_assets = ROUND(c.total_assets + gm.reward * ".C('DEFAULT_REWARD_FEE').",2)
                WHERE gm.group_id IN (" .$groupIdList. ")";

            M()->execute($sql);

            //增加流水
            $groupMemberMap['group_id'] = array('in',$groupIdList);
            $groupMemberList = D('GroupMember')->where($groupMemberMap)->select();

            foreach ($groupMemberList as $groupMember) {
                $accountDetailData[] = array('money' => round($groupMember['reward'] * C('DEFAULT_REWARD_FEE'),2), 'customer_id' => $groupMember['customer_id'],
                    'create_time' => getCurrentDate(), 'type' => C('ACCOUNT_DETAIL_TYPE.WIN'), 'refer_id' => $groupMember['group_id']);
            }

            D('AccountDetail')->addAll($accountDetailData);

            //todo 异步推送中奖团
            foreach (explode(',',$groupIdList) as $groupId) {
                curlPost('http://114.215.113.76:9502/pushWechat',array('groupId' => $groupId));
            }

        }

    }

    public function endTimeRefund() {

        $where = "p.end_time <= NOW() AND r.status = " .C('ROUND_STATUS.WAITING');

        $productList = D('Product')->alias('p')
            ->join('LEFT JOIN __ROUND__ r ON p.product_id = r.product_id')
            ->field('p.product_id, r.round_id')
            ->where($where)
            ->select();

        if(!isNullOrEmpty($productList)) {

            $roundIdList = array_column($productList,'round_id');

            $orderMap['o.round_id'] = array('in',$roundIdList);
            $orderMap['o.status'] = C('ORDER_STATUS.WAITING_OPEN');
            $orderList = D('Order')->alias('o')
                ->join('LEFT JOIN __CUSTOMER__ c ON o.customer_id = c.customer_id')
                ->join('LEFT JOIN __ROUND__ r ON o.round_id = r.round_id')
                ->join('LEFT JOIN __PRODUCT__ p ON r.product_id = p.product_id')
                ->field('o.*, c.openid, p.product_id, p.product_name')
                ->where($orderMap)
                ->select();

            if(!isNullOrEmpty($orderList)) {
                //修改余额及订单状态
                $orderIdList = rtrim(implode(',',array_column($orderList,'order_id')),',');

                //修改状态
                $sql = "UPDATE __ORDER__ o set o.status = "
                        . C("ORDER_STATUS.SYSTEM_ABANDON")
                        . " WHERE o.order_id IN (" .$orderIdList. ")";

                M()->execute($sql);

                //修改余额
                $sql = "UPDATE __CUSTOMER__ c
                        INNER JOIN (SELECT sum(number) as number,customer_id 
                            FROM __ORDER__ WHERE 
                            order_id IN (".$orderIdList.") 
                            GROUP BY customer_id) o ON c.customer_id = o.customer_id
                        SET c.total_assets = c.total_assets + o.number, 
                            c.available_assets = c.available_assets + o.number";
                M()->execute($sql);

                //修改团状态
                $groupIdList = array_values(array_unique(array_column($orderList,'group_id')));

                $groupData['status'] = C('GROUP_STATUS.SYSTEM_ABANDON');

                D('Group')->where(array('group_id' => array('in',$groupIdList)))->save($groupData);

                foreach($orderList as $order) {
                    //流水记录
                    $accountDetailData[] = array('money' => $order['number'], 'customer_id' => $order['customer_id'],
                        'create_time' => getCurrentDate(), 'type' => C('ACCOUNT_DETAIL_TYPE.REFUND'), 'refer_id' => $order['order_id']);

                    //模板消息
                    $messageData['openid'] = $order['openid'];
                    $messageData['url'] = C('ROOT_URL')."/wechat/userCenter/index";
                    $messageData['product_name'] = $order['product_name'];
                    $messageData['money'] = $order['number'];
                    $messageData['refund_money'] = $order['number'];

                    //推送退款模板消息
                    refundMessage($messageData);
                }

                //添加流水
                D('AccountDetail')->addAll($accountDetailData);

            }

            //修改产品状态
            $productIdList = array_values(array_unique(array_column($productList,'product_id')));

            $productData['is_online'] = C('DEFAULT_YN.NO');

            D('Product')->where(array('product_id' => array('in',$productIdList)))->save($productData);

        }


    }


}